package ch.ethz.fcl.metrobuzz.render.od.flow_view.interactions;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import ch.ethz.fcl.metrobuzz.controller.gui.twl.frames.TimeControlFrame;
import ch.ethz.fcl.metrobuzz.data.MBData;
import ch.ethz.fcl.metrobuzz.data.od.SelectLinkQuery;
import ch.ethz.fcl.metrobuzz.data.scene.basemap.Node;
import ch.ethz.fcl.metrobuzz.render.od.flow_view.ODFrame;
import ch.ethz.fcl.metrobuzz.render.od.flow_view.sankey.ODSankeyDiagramData;
import ch.ethz.fcl.metrobuzz.render.od.flow_view.sankey.SankeyNode;
import ch.ethz.fcl.metrobuzz.render.od.map_view.MapViewer;

public class ODPopupMenuActionListener implements ActionListener {
	private ODPopupMenu menu;
	private ODFrame frame;

	public ODPopupMenuActionListener(ODFrame frame, ODPopupMenu menu) {
		this.frame = frame;
		this.menu = menu;
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getActionCommand().equals(ODPopupMenu.HIGH_LIGHT)) {
			if (menu.selectedGroup != null) {
				MapViewer.highlightNodeList = new ArrayList<Node>();

				for (SankeyNode sn : menu.selectedGroup.getNodeList()) {
					Node node = frame.getData().getSLQ().getData().getScene()
							.getNode(sn.getId());
					MapViewer.highlightNodeList.add(node);
				}

				MapViewer.count = 0;
			}
		} else if (event.getActionCommand().equals(ODPopupMenu.HIDE)) {
			if (menu.selectedGroup != null) {
				menu.selectedGroup.hide = true;

				frame.getData().expandGroups();
				frame.getComponent().calODPositions();
				frame.repaint();
			}
		} else if (event.getActionCommand().equals(ODPopupMenu.RESET)) {
			ODSankeyDiagramData.reset = true;
			MBData.startTime = 32;
			MBData.period = 1;

			MapViewer.highlightNodeList = null;

			SelectLinkQuery slq = frame.getData().getSLQ();
			slq.update(null, MBData.startTime, MBData.period);

			TimeControlFrame timeFrame = frame.getData().getSLQ().getData()
					.getContainer().getControl().getTWLGUI().getTimeControl();
			timeFrame.reset();

			frame.getComponent().update();
			frame.repaint();
		}
	}
}
